package com.funny.learning.english.db;

import java.util.ArrayList;

import com.funny.learning.english.model.HistoryEntity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
//
//

public class HistoryDAO extends BaseDAO {

	public HistoryDAO(Context context) {
		super(context);
	}

	public ArrayList<String> getAllTopic() {
		ArrayList<String> strings = new ArrayList<String>();

		SQLiteDatabase database = getReadableDatabase();

		String sql = "select `topic` from history group by topic";

		Cursor cursor = database.rawQuery(sql, null);

		while (cursor.moveToNext()) {
			String s = cursor.getString(0);
			
			if (!strings.contains(s)) {
				strings.add(s);
				Log.d("+++++++++++++++", s);
			}
			
		}

		cursor.close();
		database.close();

		return strings;
	}

	public ArrayList<String> getAllSubtopicByTopicName(String name) {
		ArrayList<String> strings = new ArrayList<String>();

		SQLiteDatabase database = getReadableDatabase();

		String sql = "select `subtopic` from history where `topic` LIKE ?";

		Cursor cursor = database.rawQuery(sql, new String[] { name });

		while (cursor.moveToNext()) {
			String s = cursor.getString(0);
			if (!strings.contains(s)) {
				strings.add(s);
				Log.d("+++++++++++++++", s);
			}
			
		}

		cursor.close();
		database.close();

		return strings;
	}

	public ArrayList<HistoryEntity> getAllHistoryByTopicNameAndSubtopicName(
			String topic, String subtopic) {

		ArrayList<HistoryEntity> historyEntities = new ArrayList<HistoryEntity>();

		SQLiteDatabase database = getReadableDatabase();

		String sql = "SELECT * FROM `history` WHERE topic LIKE ? AND subtopic LIKE ?";

		Cursor cursor = database
				.rawQuery(sql, new String[] { topic, subtopic });

		while (cursor.moveToNext()) {
			HistoryEntity historyEntity = new HistoryEntity(cursor);
			historyEntities.add(historyEntity);
			Log.d("+++++++++++++++", historyEntity.toString());
		}

		return historyEntities;

	}

	public void saveHistory(HistoryEntity historyEntity) {
		SQLiteDatabase database = getReadableDatabase(); 
		ContentValues values = new ContentValues(); 
		values.put("thumb", historyEntity.getThumb()); 
		values.put("game", historyEntity.getGame()); 
		values.put("dung_count", historyEntity.getDung_count()); 
		values.put("tong_count", historyEntity.getTong_count()); 
		values.put("time", historyEntity.getTime()); 
		values.put("topic", historyEntity.getTopic()); 
		values.put("subtopic", historyEntity.getSubtopic()); 
		database.insert("history", null, values); 
		database.close();
	}

}
